package com.woniu.his.sms.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.bms.client.BmsQueryAmountForStaffClient;
import com.woniu.his.bms.client.result.QueryAmountForStaffResult;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import com.woniu.his.sms.common.SmsResultCode;
import com.woniu.his.sms.common.enums.SmsStatusEnums;
import com.woniu.his.sms.infra.model.Dept;
import com.woniu.his.sms.infra.model.Staff;
import com.woniu.his.sms.infra.model.WorkloadRecord;
import com.woniu.his.sms.infra.mapper.WorkloadRecordMapper;
import com.woniu.his.sms.service.DeptService;
import com.woniu.his.sms.service.StaffService;
import com.woniu.his.sms.service.WorkloadRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.sms.service.dto.QueryAllDeptWorkLoadDto;
import com.woniu.his.sms.service.dto.QueryWorkLoadByDeptIdDto;
import com.woniu.his.sms.service.param.QueryAllDeptWorkLoadServiceParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工作量记录表 服务实现类
 * </p>
 *
 * @author cb
 * @since 2022-11-25
 */
@Service
public class WorkloadRecordServiceImpl extends ServiceImpl<WorkloadRecordMapper, WorkloadRecord> implements WorkloadRecordService {

    @Resource
    private StaffService staffService;

    @Resource
    private BmsQueryAmountForStaffClient bmsQueryAmountForStaffClient;

    @Resource
    private DeptService deptService;

    @Override
    public void addWorkLoadRecordForStaff(Long staffId) {

        //判断员工是否存在
        Staff staff = staffService.getById(staffId);
        if (staff == null) throw new HisException(SmsResultCode.DOCTOR_NOT_EXIST);
        if (staff.getStaffStatus().equals(SmsStatusEnums.DELETE.getCode())||(staff.getStaffStatus().equals(SmsStatusEnums.FROZEN.getCode()))){
            throw new HisException(SmsResultCode.STAFF_DELETED);
        }

        //判断是否是当天的，如果是就更新，不是则新建
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        DateTime time= DateUtil.parse(format.format(new Date()));
        QueryWrapper<WorkloadRecord> wrapper=new QueryWrapper<WorkloadRecord>();
        wrapper.eq("date",time).eq("staff_id",staffId);
        WorkloadRecord one = getOne(wrapper);
        //新建记录
        if (one==null) {
            //工作量统计
            WorkloadRecord workloadRecord = new WorkloadRecord();
            //创建时间
            workloadRecord.setCreateTime(new Date());
            //员工ID
            workloadRecord.setStaffId(staffId);
            //日期
            workloadRecord.setDate(time);
            //部门ID
            workloadRecord.setDeptId(staff.getDeptId());
            //记录表状态
            workloadRecord.setWorkloadRecordStatus(SmsStatusEnums.ON.getCode());

            //调bms接口 查金额
            Result<QueryAmountForStaffResult> s = bmsQueryAmountForStaffClient.QueryAmountForStaff(staffId, time.toString());
            if (s.getData()==null) {
                save(workloadRecord);
                return;
            }
            workloadRecord.setMedicineAmount(s.getData().getMedicineAmount());
            workloadRecord.setHerbalAmount(s.getData().getHerbalAmount());
            workloadRecord.setCheckAmount(s.getData().getCheckAmount());
            workloadRecord.setDispositionAmount(s.getData().getDispositionAmount());
            workloadRecord.setRegistrationAmount(s.getData().getRegistrationAmount());
            workloadRecord.setTestAmount(s.getData().getTestAmount());
            workloadRecord.setAmount(s.getData().getAmount());
            workloadRecord.setRegistrationNum(s.getData().getRegistrationNum());
            workloadRecord.setExcuteCheckAmount(s.getData().getExcuteCheckAmount());
            workloadRecord.setExcuteTestAmount(s.getData().getExcuteTestAmount());
            workloadRecord.setExcuteDispositionAmount(s.getData().getExcuteDispositionAmount());
            workloadRecord.setExcuteNum(s.getData().getExcuteNum());
            save(workloadRecord);
        }else {
            //工作量统计

            //调bms接口 查金额
            Result<QueryAmountForStaffResult> q = bmsQueryAmountForStaffClient.QueryAmountForStaff(staffId, time.toString());
            if (q.getData()==null) {
                updateById(one);
                return;
            }
            one.setMedicineAmount(q.getData().getMedicineAmount());
            one.setHerbalAmount(q.getData().getHerbalAmount());
            one.setCheckAmount(q.getData().getCheckAmount());
            one.setDispositionAmount(q.getData().getDispositionAmount());
            one.setRegistrationAmount(q.getData().getRegistrationAmount());
            one.setTestAmount(q.getData().getTestAmount());
            one.setAmount(q.getData().getAmount());
            one.setRegistrationNum(q.getData().getRegistrationNum());
            one.setExcuteCheckAmount(q.getData().getExcuteCheckAmount());
            one.setExcuteTestAmount(q.getData().getExcuteTestAmount());
            one.setExcuteDispositionAmount(q.getData().getExcuteDispositionAmount());
            one.setExcuteNum(q.getData().getExcuteNum());
            updateById(one);
        }


    }

    @Override
    public QueryAllDeptWorkLoadDto queryAllDeptWorkLoad(QueryAllDeptWorkLoadServiceParam serviceParam) {

        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate=DateUtil.parse(format.format(serviceParam.getStartTime()));
        Date endDate=DateUtil.parse(format.format(serviceParam.getEndTime()));

        //成药总金额
        BigDecimal medicineAmount= new BigDecimal(0);
        //草药总金额
        BigDecimal herbalAmount = new BigDecimal(0);
        //检查总金额
        BigDecimal checkAmount = new BigDecimal(0);
        //检验总金额
        BigDecimal testAmount = new BigDecimal(0);
        //处置总金额
        BigDecimal dispositionAmount = new BigDecimal(0);
        //挂号总金额
        BigDecimal registrationAmount = new BigDecimal(0);
        //总金额
        BigDecimal totalAmount = new BigDecimal(0);
        //挂号数量
        Long registrationNum= new Long(0);
        //执行检查的金额
        BigDecimal excuteCheckAmount = new BigDecimal(0);
        //执行检验的金额
        BigDecimal excuteTestAmount = new BigDecimal(0);
        //执行处置的金额
        BigDecimal excuteDispositionAmount = new BigDecimal(0);
        //执行的总人数
        Long excuteRegistrationNum= new Long(0);

        //查出有效的
        QueryWrapper<WorkloadRecord> wrapper=new QueryWrapper<>();
        wrapper.eq("workload_record_status",SmsStatusEnums.ON.getCode());

        List<WorkloadRecord> list = list(wrapper);

        for (WorkloadRecord workloadRecord : list) {
            if (workloadRecord.getDate().after(startDate)&&workloadRecord.getDate().before(endDate)){
                medicineAmount=medicineAmount.add(workloadRecord.getMedicineAmount());
                herbalAmount=herbalAmount.add(workloadRecord.getHerbalAmount());
                checkAmount=checkAmount.add(workloadRecord.getCheckAmount());
                testAmount=testAmount.add(workloadRecord.getTestAmount());
                dispositionAmount=dispositionAmount.add(workloadRecord.getDispositionAmount());
                registrationAmount=registrationAmount.add(workloadRecord.getRegistrationAmount());
                totalAmount=totalAmount.add(workloadRecord.getAmount());
                registrationNum=registrationNum+workloadRecord.getRegistrationNum();
                excuteCheckAmount=excuteCheckAmount.add(workloadRecord.getExcuteCheckAmount());
                excuteTestAmount=excuteTestAmount.add(workloadRecord.getExcuteTestAmount());
                excuteDispositionAmount=excuteDispositionAmount.add(workloadRecord.getExcuteDispositionAmount());
                excuteRegistrationNum=excuteRegistrationNum+workloadRecord.getExcuteNum();
            }
        }

        QueryAllDeptWorkLoadDto dto=new QueryAllDeptWorkLoadDto();
        dto.setMedicineAmount(medicineAmount);
        dto.setHerbalAmount(herbalAmount);
        dto.setCheckAmount(checkAmount);
        dto.setDispositionAmount(dispositionAmount);
        dto.setRegistrationAmount(registrationAmount);
        dto.setTestAmount(testAmount);
        dto.setAmount(totalAmount);
        dto.setRegistrationNum(registrationNum);
        dto.setExcuteCheckAmount(excuteCheckAmount);
        dto.setExcuteTestAmount(excuteTestAmount);
        dto.setExcuteDispositionAmount(excuteDispositionAmount);
        dto.setExcuteNum(excuteRegistrationNum);

        return dto;
    }

    @Override
    public QueryWorkLoadByDeptIdDto queryWorkLoadByDeptId(Long deptId) {

        //判断科室存在或有效
        Dept dept = deptService.getById(deptId);
        if (dept==null) throw new HisException(SmsResultCode.DEPT_NOT_EXIST);
        if (dept.getDeptStatus().equals(SmsStatusEnums.OFF.getCode())) throw new HisException(SmsResultCode.DEPT_NOT_EXIST);

        //成药总金额
        BigDecimal medicineAmount= new BigDecimal(0);
        //草药总金额
        BigDecimal herbalAmount = new BigDecimal(0);
        //检查总金额
        BigDecimal checkAmount = new BigDecimal(0);
        //检验总金额
        BigDecimal testAmount = new BigDecimal(0);
        //处置总金额
        BigDecimal dispositionAmount = new BigDecimal(0);
        //挂号总金额
        BigDecimal registrationAmount = new BigDecimal(0);
        //总金额
        BigDecimal totalAmount = new BigDecimal(0);
        //挂号数量
        Long registrationNum= new Long(0);
        //执行检查的金额
        BigDecimal excuteCheckAmount = new BigDecimal(0);
        //执行检验的金额
        BigDecimal excuteTestAmount = new BigDecimal(0);
        //执行处置的金额
        BigDecimal excuteDispositionAmount = new BigDecimal(0);
        //执行的总人数
        Long excuteRegistrationNum= new Long(0);

        QueryWrapper<WorkloadRecord> wrapper=new QueryWrapper<>();
        wrapper.eq("dept_id",dept).eq("workload_record_status",SmsStatusEnums.ON.getCode());
        List<WorkloadRecord> list = list(wrapper);
        if (list.isEmpty()) {
            QueryWorkLoadByDeptIdDto dto=new QueryWorkLoadByDeptIdDto();
            dto.setDeptId(deptId);
            dto.setMedicineAmount(medicineAmount);
            dto.setHerbalAmount(herbalAmount);
            dto.setCheckAmount(checkAmount);
            dto.setDispositionAmount(dispositionAmount);
            dto.setRegistrationAmount(registrationAmount);
            dto.setTestAmount(testAmount);
            dto.setAmount(totalAmount);
            dto.setRegistrationNum(registrationNum);
            dto.setExcuteCheckAmount(excuteCheckAmount);
            dto.setExcuteTestAmount(excuteTestAmount);
            dto.setExcuteDispositionAmount(excuteDispositionAmount);
            dto.setExcuteNum(excuteRegistrationNum);
            return dto;
        }else{
            for (WorkloadRecord workloadRecord : list) {
                    medicineAmount=medicineAmount.add(workloadRecord.getMedicineAmount());
                    herbalAmount=herbalAmount.add(workloadRecord.getHerbalAmount());
                    checkAmount=checkAmount.add(workloadRecord.getCheckAmount());
                    testAmount=testAmount.add(workloadRecord.getTestAmount());
                    dispositionAmount=dispositionAmount.add(workloadRecord.getDispositionAmount());
                    registrationAmount=registrationAmount.add(workloadRecord.getRegistrationAmount());
                    totalAmount=totalAmount.add(workloadRecord.getAmount());
                    registrationNum=registrationNum+workloadRecord.getRegistrationNum();
                    excuteCheckAmount=excuteCheckAmount.add(workloadRecord.getExcuteCheckAmount());
                    excuteTestAmount=excuteTestAmount.add(workloadRecord.getExcuteTestAmount());
                    excuteDispositionAmount=excuteDispositionAmount.add(workloadRecord.getExcuteDispositionAmount());
                    excuteRegistrationNum=excuteRegistrationNum+workloadRecord.getExcuteNum();
            }
            QueryWorkLoadByDeptIdDto dto=new QueryWorkLoadByDeptIdDto();
            dto.setMedicineAmount(medicineAmount);
            dto.setHerbalAmount(herbalAmount);
            dto.setCheckAmount(checkAmount);
            dto.setDispositionAmount(dispositionAmount);
            dto.setRegistrationAmount(registrationAmount);
            dto.setTestAmount(testAmount);
            dto.setAmount(totalAmount);
            dto.setRegistrationNum(registrationNum);
            dto.setExcuteCheckAmount(excuteCheckAmount);
            dto.setExcuteTestAmount(excuteTestAmount);
            dto.setExcuteDispositionAmount(excuteDispositionAmount);
            dto.setExcuteNum(excuteRegistrationNum);

            return dto;
        }
    }
}
